From 8cfff2ef57c2886fbe630f21f9c060af08e2af58 Mon Sep 17 00:00:00 2001 From: Marco Leogrande Date: Wed, 21 Jun 2017 23:40:15 -0700 Subject: [PATCH] garmin_fit: add handling of global UTC offset Some devices may record timestamps in system time (i.e. boot time), rather than system time. As mentioned in Github issue #54 and in the upstream documentation, we can recognize such a scenario when timestamp is < 0x10000000. Field 4 under the global ID for device settings records the difference between system time and UTC time (this is called global UTC offset in the upstream documentation). Add code to store the global UTC offset in a new global variable, and correctly handle all timestamps with the new information. --- garmin_fit.cc | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/garmin_fit.cc b/garmin_fit.cc index 57c8a2a1f..3fb471295 100644 --- a/garmin_fit.cc +++ b/garmin_fit.cc @@ -57,6 +57,7 @@ static struct { int endian; route_head* track; uint32_t last_timestamp; + uint32_t global_utc_offset; fit_message_def message_def[16]; } fit_data; @@ -135,6 +136,8 @@ fit_parse_header(void) // Unused according to Ingo Arndt gbfgetuint16(fin); } + + fit_data.global_utc_offset = 0; } static uint8_t @@ -352,9 +355,31 @@ fit_parse_data(fit_message_def* def, int time_offset) if (global_opts.debug_level >= 7) { debug_print(7,"%s: parsing fit data: timestamp=%d\n", MYNAME, val); } - fit_data.last_timestamp = timestamp = val; + timestamp = val; + // if the timestamp is < 0x10000000, this value represents + // system time; to convert it to UTC, add the global utc offset to it + if (timestamp < 0x10000000) + timestamp += fit_data.global_utc_offset; + fit_data.last_timestamp = timestamp; } else { switch (def->global_id) { + case 0: // device settings message + switch (f->id) { + case 4: + if (global_opts.debug_level >= 7) { + debug_print(7,"%s: parsing fit data: global utc_offset=%d\n", MYNAME, val); + } + fit_data.global_utc_offset = val; + break; + default: + if (global_opts.debug_level >= 1) { + debug_print(1, "%s: unrecognized data type in GARMIN FIT device settings: f->id=%d\n", MYNAME, f->id); + } + break; + } // switch (f->id) + // end of case def->global_id = 0 + break; + case 20: // record message - trkType is a track switch (f->id) { case 0: -- 2.30.2